package com.example.server.service.base.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.example.server.common.config.aliyun.oss.OSSConfig;
import com.example.server.common.constant.Result;
import com.example.server.common.utils.oss.OSSUtil;
import com.example.server.service.base.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author china.fuyao@outlook.com
 * @date 2020-04-08 14:37
 */
@Slf4j
@Service("imageService")
public class ImageServiceImpl implements ImageService {
    /**
     * 获取图片上传签名
     *
     * @return
     */
    @Override
    public Result getUploadImgPolicy() {
        return OSSUtil.getUploadImgPolicy();
    }

    /**
     * 服务器视频上传
     *
     * @param file
     * @return
     */
    @Override
    public String uploadVideoByServer(MultipartFile file) {
        //
        log.info("文件上传");
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            String img = OSSUtil.uploadVideo(inputStream);
            return img;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    /**
     * 服务器图片上传
     *
     * @param file
     * @return
     */
    @Override
    public String uploadImgByServer(MultipartFile file) {
        log.info("文件上传");
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            String img = OSSUtil.uploadImg(inputStream);
            return img;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * layedit图片上传
     *
     * @param file
     * @return
     */
    @Override
    public JSONObject uploadImgByLay(MultipartFile file) {
        String imgUrl = uploadImgByServer(file);
        JSONObject data = new JSONObject();
        JSONObject json = new JSONObject();
        if (StrUtil.isNullOrUndefined(imgUrl)) {
            data.put("code", 1);
            data.put("msg", "图片上传失败，请重试");
            json.put("src", null);
            json.put("name", null);
            data.put("data", json);
            return data;
        }
        data.put("code", 0);
        data.put("msg", "success");
        json.put("src", "https://" + OSSConfig.bucket + "." + OSSConfig.endpoint + "/" + imgUrl);
        json.put("name", "name");
        data.put("data", json);
        return data;
    }

    private String imageBase64(MultipartFile file) throws IOException {
        String imageBase64 = file.getOriginalFilename() + "," + Base64.encode(file.getBytes());
        return imageBase64;
    }
}
